package com.cloudfun.campusshare.configuration.jpa.conventer;

import com.cloudfun.campusshare.common.constant.ReplenishStatus;

import javax.persistence.AttributeConverter;

/**
 * @author wangxiyuan
 * @description 枚举类中int类型插入数据库
 * @createDate 2021/5/28 10:08
 **/
public class EnumIntConventer implements AttributeConverter<ReplenishStatus,Integer> {
    @Override
    public Integer convertToDatabaseColumn(ReplenishStatus replenishStatus) {
        if(replenishStatus == null){
            throw new RuntimeException("Unknown replenishStatus text : " + replenishStatus);
        }
        return replenishStatus.getCode();
    }

    @Override
    public ReplenishStatus convertToEntityAttribute(Integer integer) {
        for(ReplenishStatus s : ReplenishStatus.values()){
            if(s.getCode() == integer){
                return s;
            }
        }
        return null;
    }
}
